package testcase.app;

import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import api.app.VerificationCodeForPassword;
import api.app.VerificationPhoneCode;
import cn.bmyi.sms.Sms;
import common.Host;
import common.TestData;

/**
 * 帮忙医APP登录VerificationPhoneCode接口测试用例
 * @author lipeng
 *
 */

public class TestVerificationPhoneCode
{
	/**
	 * 帮忙医APP校验校验码VerificationPhoneData接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
   @DataProvider(name = "Test")
   public static String[][] testVerificationPhoneCodeDataProvider() throws Exception{
	   Host.setup("Test");
	   TestData td = new TestData("app/TestVerificationPhoneCode.xml");
	   return td.getParam();
   }
   
   /**
    * 帮忙医APP校验校验码VerificationPhoneData接口
    * @param caseName  -用例名称
    * @param moblie    -手机号码
    * @param code      -验证码
    * @throws Exception
    */
   @Test(dataProvider="Test",groups={"Test"})
   public void testVerificationPhoneCodeTestCase(String caseName, String mobile,String code)throws Exception{
	   
	   String getCodeResult = VerificationCodeForPassword.post(mobile);
	   Boolean isMobileExist = new JSONObject(getCodeResult).getJSONObject("Data").getBoolean("MobileExist"); 
	   if(!isMobileExist){  //判断验证码是否获取成功，如果没有获取成功，测试用例执行失败
		   Assert.fail("该手机号已注册");
	   }
	   if (caseName.equals("Success"))
	   {
		   code=Sms.getVerificationCodeRepeatedly(mobile);
	   }
	   String VeriCodeResult = VerificationPhoneCode.post(mobile,code);

	 //调用接口并返回数据转换成json格式
	   JSONObject jsonResult;
	   if(VeriCodeResult==null || VeriCodeResult.isEmpty()){
		   jsonResult = null;
	   }
	   else {
		   jsonResult = new JSONObject(VeriCodeResult);
	   }
	   
	   //设置检查点
	   switch (caseName)
	{
		case "Success":
			Assert.assertEquals(jsonResult.getJSONObject("Data").getString("verificationstatus"), "Success");
			break;
        case "CodeError":
        	Assert.assertEquals(jsonResult.getJSONObject("Data").getString("verificationstatus"), "Fail");
			break;
        case "NoCode":
        	Assert.assertEquals(jsonResult.getJSONObject("Data").getString("verificationstatus"), "Fail");
	        break;
		default:
			Assert.fail();
			break;
	}
   }
}
